package cc.yugu.yhdw.webserver.web.controller.security.basic;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.domain.basic.Role;
import cc.yugu.yhdw.common.domain.basic.User;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.service.basic.RoleOperService;
import cc.yugu.yhdw.webserver.service.basic.RoleService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/security/basic/role")
public class RoleController extends SecurityController {
    @Autowired
    RoleService roleService;
    @Autowired
    RoleOperService roleOperService;

    @RequestMapping(value = "index.htm")
    public void index(Model model) throws Exception {
       model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_01_07.getValue());
    }

    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(Role search, HttpServletRequest request) {
        if (getSessionUser(request).getType() == User.Type.AGENT.getValue()||getSessionUser(request).getType() == User.Type.AGENCY.getValue()) {
            search.setAgentId(getSessionUser(request).getAgentId());
        }
        return PageResult.successResult(roleService.findPage(search));
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON_ARRAY)
    @RequestMapping(value = "tree.htm")
    public void tree(Integer id, HttpServletResponse response) throws Exception {
        response.setContentType(ConstEnum.ContentType.JSON.getValue());
        roleService.tree(id, response.getOutputStream());
    }

    @RequestMapping("find_all.htm")
    @ViewModel(ViewModel.JSON_ARRAY)
    @ResponseBody
    public void findAllUser(int agentId, HttpServletResponse response) throws IOException {
        response.setContentType(ConstEnum.ContentType.JSON.getValue());
        roleService.tree(agentId, response.getOutputStream());
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping("add.htm")
    public void add() {
    }

    @ResponseBody
    @RequestMapping(value = "create.htm")
    public ExtResult create(Role role, String operIds) {
        String[] str = StringUtils.split(operIds, ",");
        List<Integer> operList = new ArrayList<Integer>();

        for(String oper : str) {
            operList.add(Integer.parseInt(oper));
        }

        role.setOperList(operList);
        role.setCreateTime(new Date());
        return roleService.create(role);
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping("edit.htm")
    public String edit(int id, Model model) {
        Role role = roleService.find(id);
        if(role == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }

        model.addAttribute("entity", role);
        return "/security/basic/role/edit";
    }

    @ResponseBody
    @RequestMapping(value = "update.htm")
    public ExtResult update(Role role, String operIds) {
        String[] str = StringUtils.split(operIds, ",");
        List<Integer> operList = new ArrayList<Integer>();

        for(String oper : str) {
            operList.add(Integer.parseInt(oper));
        }

        role.setOperList(operList);

        return roleService.update(role);
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping("view.htm")
    public String view(int id, Model model) {
        Role role = roleService.find(id);
        if(role == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }

        model.addAttribute("entity", role);
        return "/security/basic/role/view";
    }
    @ResponseBody
    @RequestMapping(value = "delete.htm")
    public ExtResult delete(int id) {
        return roleService.delete(id);
    }

    @ResponseBody
    @RequestMapping(value = "batch_delete.htm")
    public ExtResult batchDelete(int[] id) {
        return roleService.batchDelete(id);
    }

}
